%--------------------------------------------------------------------------
%This program extracts the largest strongly connected set of establishments
%from the BeH. Note only establishments that hire from N are considered.
%--------------------------------------------------------------------------

clc;
clear all;

cd '\\employers\matlab\sconn_idfy';
addpath('\\employers\matlab\functions');


%-------------------------------------------------------------------------%
% create looping variables for sample windows
%-------------------------------------------------------------------------%

%empty vector to store versions 
vervec = ones(30,1);

%declare custom windows 
c=1; 
for w = 7
    for y = 1997:1:2019
        for s = 1:2
            if y + (w-1)/2 <2019 && y - (w-1)/2 >1997   
                ver = y*100 + w*10 + s;
                vervec(c) = ver;
                c = c+1;
            end
        end
    end
end
clear c;

%-------------------------------------------------------------------------%
%loop over sample windows
%-------------------------------------------------------------------------%

for i = 1:length(vervec) 
    ver = vervec(i)
    
    %--------------------------------------------------------------------------
    % All moves dataset
    %--------------------------------------------------------------------------

    fnamein = ['flows_ver',num2str(ver),'.csv'];
    fnameout = ['sconn_idfy_ver',num2str(ver),'.csv'];
    clear ver

    all_moves = csvread(fnamein,1,0);
    %drop the row of flows
    all_moves = all_moves(:,1:2);

    %find the strongly connected set
    D = [all_moves ones(length(all_moves),1)];
    A = spconvert(D); %adjacency matrix
    clear D;
    [sci,sizes,rp,ci]=scomponents(A);
    clear A;
    [csize,cind] = max(sizes);
    sconn = sci==cind; %identify estabs in the largest strongly connected set
    %output the id's and indicators of belonging to the strongly connected set
    %to csv
    sconn = [(1:1:length(sconn))' sconn];
    dlmwrite(fnameout,sconn,'precision','%10.9g');
    
end